package com.wu.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.mapper.ClassifyMapper;
import com.wu.pojo.Classify;
import com.wu.pojo.model.UserToken;
import com.wu.service.ClassifyService;
import com.wu.util.MyUtil;
import com.wu.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-19 20:32
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {
    @Autowired
    private ClassifyMapper classifyMapper;

    @Autowired
    private MyUtil myUtil;

    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 通过用户Id获全部的文章分类名称
     *
     * @return
     */
    @Override
    public List<Classify> getClassifyByUserId(String user_id) {
        List<Classify> classifyList = null;
        if (StringUtils.isEmpty(user_id)) {
            UserToken loginUser = tokenUtils.parseTokenToUserToken();
            String userId = loginUser.getUserId();
            classifyList = classifyMapper.getClassifyByUserId(userId);
        } else {
            classifyList = classifyMapper.getClassifyByUserId(user_id);
        }
        return classifyList;
    }

    @Override
    public List<Classify> getAllClassify() {
        return classifyMapper.getAllClassify();
    }

    @Override
    public List<Classify> getAllArticleNotNullClassify() {
        List<Classify> notNullClassify = classifyMapper.getAllArticleNotNullClassify();
        return notNullClassify;
    }

    @Override
    public void addClassify(String classifyName) {
        if (StringUtils.isEmpty(classifyName)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR);
        }

        int i = classifyMapper.addClassify(IdWorker.get32UUID(), classifyName);
        if (i < 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "添加分类失败");
        }
    }

    @Override
    public Classify getClassifyByName(String classifyName) {
        if (StringUtils.isEmpty(classifyName)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR);
        }
        Classify classify = classifyMapper.getClassifyByName(classifyName);
        if (classify == null) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "分类不存在");
        }
        return classify;
    }

    @Override
    public Classify getClassifyById(String classifyId) {
        if (StringUtils.isEmpty(classifyId)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR);
        }
        Classify classify = classifyMapper.getClassifyById(classifyId);
        if (classify == null) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "分类不存在");
        }
        return classify;
    }
}
